﻿using System;
using System.Runtime.Caching;
using Chat.Models.Services;
using Chat.Models;

public class TokenStore
{
    private static readonly object CacheLock = new object();

    public static SessionMoudel GetCachedData(string CacheKey)
    {
        MemoryCache cache = MemoryCache.Default;
        return cache.Get(CacheKey) as SessionMoudel;
    }

    public static void SetCachedData(string CacheKey ,  object data)
    {
        MemoryCache cache = MemoryCache.Default;

        lock (CacheLock)
        {
            CacheItemPolicy policy = new CacheItemPolicy
            {
                AbsoluteExpiration = DateTimeOffset.Now.AddDays(1)
            };

            cache.Set(CacheKey, data, policy);
        }
    }
    public static void ExtendCacheExpiration(string CacheKey)
    {
        MemoryCache cache = MemoryCache.Default;

        lock (CacheLock)
        {
            if (cache.Contains(CacheKey))
            {
                CacheItem existingCacheItem = cache.GetCacheItem(CacheKey);
                CacheItemPolicy policy = new CacheItemPolicy
                {
                    AbsoluteExpiration = DateTimeOffset.Now.AddDays(1)
                };

                cache.Set(CacheKey, existingCacheItem.Value, policy);
            }
        }
    }
}
